package com.seven.commons;

import lombok.extern.slf4j.Slf4j;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.config.Registry;
import org.apache.http.config.RegistryBuilder;
import org.apache.http.config.SocketConfig;
import org.apache.http.conn.DnsResolver;
import org.apache.http.conn.HttpConnectionFactory;
import org.apache.http.conn.ManagedHttpClientConnection;
import org.apache.http.conn.routing.HttpRoute;
import org.apache.http.conn.socket.ConnectionSocketFactory;
import org.apache.http.conn.socket.LayeredConnectionSocketFactory;
import org.apache.http.conn.socket.PlainConnectionSocketFactory;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.impl.conn.DefaultHttpResponseParserFactory;
import org.apache.http.impl.conn.ManagedHttpClientConnectionFactory;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.impl.conn.SystemDefaultDnsResolver;
import org.apache.http.impl.io.DefaultHttpRequestWriterFactory;

import javax.net.ssl.SSLContext;
import java.security.NoSuchAlgorithmException;

/**
 * @projectname:common-utils
 * @author: sevenDay
 * @date: 2019/7/23 20:00
 * @desc:
 */
@Slf4j
public class SevenHttpPool {
    private static SevenHttpPool sevenHttp = new SevenHttpPool();
    private static PoolingHttpClientConnectionManager cm = null;
    static{
        LayeredConnectionSocketFactory sslsf = null;
        try{
            sslsf = new SSLConnectionSocketFactory(SSLContext.getDefault());
        }catch(NoSuchAlgorithmException e){
            log.error("创建SSL连接失败...");
        }
        Registry<ConnectionSocketFactory> sRegistry = RegistryBuilder.<ConnectionSocketFactory>create()
                .register("https", SSLConnectionSocketFactory.getSystemSocketFactory())
                .register("http",  PlainConnectionSocketFactory.INSTANCE)
                .build();
        HttpConnectionFactory<HttpRoute, ManagedHttpClientConnection> connectionHttpConnectionFactory =
               new ManagedHttpClientConnectionFactory(
                       DefaultHttpRequestWriterFactory.INSTANCE,
                       DefaultHttpResponseParserFactory.INSTANCE
               );
        DnsResolver dnsResolver= SystemDefaultDnsResolver.INSTANCE;

        cm = new PoolingHttpClientConnectionManager(sRegistry,connectionHttpConnectionFactory,dnsResolver);
        //默认为Socket配置
        SocketConfig defaultSocketConfig = SocketConfig.custom().setTcpNoDelay(true).build();
        cm.setDefaultSocketConfig(defaultSocketConfig);
        // 设置最大的连接数
        cm.setMaxTotal(300);
        // 设置每个路由的基础连接数【默认，每个路由基础上的连接不超过2个，总连接数不能超过20】
        cm.setDefaultMaxPerRoute(100);
        //在从连接池获取连接时，连接不活跃多长时间后需要进行一次验证，默认为2s
        cm.setValidateAfterInactivity(5*1000);
    }
    private SevenHttpPool(){
    }
    public static SevenHttpPool getInstance(){
        return sevenHttp;
    }

    public PoolingHttpClientConnectionManager getPoolManager(){
        return cm;
    }


}
